package com.sunseagear.wind.modules.sms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sms.entity.SmsTemplate;
import com.sunseagear.wind.modules.sms.entity.query.SmsTemplateQuery;
import com.sunseagear.wind.modules.sms.service.ISmsTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sms.controller
 * @title: 短信模板控制器
 * @description: 短信模板控制器 * @date: 2018-09-14 09:47:35
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("sms/template")
@Log(title = "短信模板")
@RequiredArgsConstructor
public class SmsTemplateController extends BaseBeanController {

    private final ISmsTemplateService smsTemplateService;


    /**
     * 列表分页
     * @param query 条件对象
     * @return 查询到的列表
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sms:template:list")
    public RPage<SmsTemplate> list(SmsTemplateQuery query) {
        //加入条件
        QueryWrapper<SmsTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SmsTemplate::getCreateDate)
                .like(ObjectUtils.isNotEmpty(query.getCode()),SmsTemplate::getCode, query.getCode())
                .like(ObjectUtils.isNotEmpty(query.getName()),SmsTemplate::getName, query.getName());
        // 预处理
        Page<SmsTemplate> pageBean = smsTemplateService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 添加
     * @param entity 实体对象
     * @return 操作结果
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sms:template:add")
    public R<Void> add(@Validated @RequestBody SmsTemplate entity ) {
        String templateCode = StringUtils.randomString(10);
        entity.setCode(templateCode);
        smsTemplateService.save(entity);
        return R.success("添加成功");
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return 操作结果
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("sms:template:update")
    public R<Void> update(@Validated @RequestBody SmsTemplate entity) {
        smsTemplateService.saveOrUpdate(entity);
        return R.success("更新成功");
    }

    /**
     * 删除
     * @param idList 主键集合
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sms:template:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        smsTemplateService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
